﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using DoeObjects;
using System.Web.Mvc;

namespace DoeLibsMVC.Models
{
    /// <summary>
    /// model for only a loanable to a title. The title must exists
    /// </summary>
    public class EditLoanableBasicModel
    {
        [Required]
        [Display(Name = "DoeLibs-Identity")]
        public string Barcode { get; set; }

        [Required]
        [Display(Name = "Room")]
        public string Location { get; set; }

        [Required]
        [Display(Name = "Location category")]
        public string Category
        {
            get
            {
                if (CategoryObject == null)
                {
                    CategoryObject = new LoanableCategory();
                }
                return CategoryObject.Name;
            }
            set
            {
                if (value != null)
                {
                    //check if user owns the loanablecategory:
                    User user = Security.SecurityHelper.getCurrentUser();
                    LoanableCategory cat = LoanableCategory.getByName(value.Trim(), (user != null) ? user.UserId : 0);
                    if (cat == null)
                    {
                        cat = new LoanableCategory();
                        cat.Name = value.Trim();
                        cat.Owner = user;
                        cat.save();
                    }

                    CategoryObject = cat;
                }
            }
        }

        public LoanableCategory CategoryObject { get; set; }
    }
}